<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\FourRecord;
use App\Models\Fourbusiness;
use App\Models\FourPatient;
use App\Models\FourPay;
use App\Models\FourSource;
use App\Models\FourType;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class FourRecordController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new FourRecord(['es','source','type','pay']), function (Grid $grid) {
           // $grid->column('id')->sortable();
            $grid->column('created_at');
            $grid->column('order_id','订单编号');
            $grid->column('es.patient_name','患者姓名');
            $grid->column('visit_id','就诊卡号');
            $grid->column('source.source_name','来源');
            $grid->column('type.bussiness_name','业务类型');
            $grid->column('pay.pay_name','支付方式');
            $grid->column('money');
            $grid->column('status')->using([0=>'<font style="color: green">已支付</font>',1=>'<font style="color: red">未支付</font>']);
            $grid->column('balance');
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableRefreshButton();
            $grid->disableCreateButton();
            $grid->disableFilterButton();
            $grid->export()->titles('门诊充值记录')->filename('门诊充值记录')->rows(function ($rows){
                foreach ($rows as $index => &$row){
                    if ($row['status']==0){
                        $row['status']='已支付';
                    }elseif ($row['status']==1){
                        $row['status']='未支付';
                    }
                }
                return $rows;
            });
          //  $grid->column('updated_at')->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand();
                $filter->panel();
                $filter->between('created_at', '选择日期')->datetime()->width(200);
                $filter->equal('es.patient_name','患者姓名')->width(3);
                $filter->equal('visit_id','就诊卡号')->width(3);
            });

            /*$grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });*/
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        Admin::css('/css/card.css');
        $data = \App\Models\FourRecord::with(['es','source','type','pay'])->find($id);
        return admin_view('record',compact('data'));
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new FourRecord(), function (Form $form) {
           // $form->display('id');
            $form->display('created_at');
            $form->select('order_id');
            $form->select('patient_id')->options(FourPatient::pluck('patient_name','id')->toArray());
            $form->select('visit_id');
            $form->select('source_id')->options(FourSource::pluck('source_name','id')->toArray());
            $form->select('business_type')->options(Fourbusiness::pluck('bussiness_name','id')->toArray());
            $form->select('pay_type')->options(FourPay::pluck('pay_name','id')->toArray());
            $form->text('money');
            $form->radio('status')->options(['已支付','未支付'])->default(0);
            $form->text('balance');
           // $form->display('updated_at');
        });
    }
}
